package com.pignest.hotel.ui.income;

import java.util.Date;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.Memberchargehistrory;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbCloseAccounts;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.definition.ChargeOpType;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.definition.PayMethod;
import com.pignest.hotel.service.IncomeService;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class InComeHDCheckOut extends BaseCheckOutWindow {
	private static final long serialVersionUID = -9084538837809804352L;
	private IncomeService incomeService;
	private MemberService memberService;
	/***合计项***/
	private Label aStorage;
	private Label aUsed;
	private Label aPayed;
	private Label aAvailable;
	private Label aLivingTime;
	private Label aLeavingTime;
	
	private Label roomType;
	private Label openType;
	
	private Textbox roomName;
	private Label storage;
	private Label used;
	private Label payed;
	private Label available;//当前余额

	private Textbox adjustPrice;

	
	private Listbox selectLivingList;//选择结账人与房间
	
	private Listbox paymentMethod;//结账类型
	private Listbox invoiceListbox;//是否开票

	private Textbox mCardNumber;//会员卡号
	
	private Textbox livingTime;//到达时间
	private Textbox leaveTime;//离开时间
	
	private Button okBtn;
	private Label opTip;
	private Grid memberGrid;
	private Date earlyTime;
	private Date checkTime;
	

	private Textbox aAccount;
	private Label account;
	
	public InComeHDCheckOut(){
		incomeService =Services.getIncomeService();
		memberService=Services.getMemberService();
		addEventListener("onCreate", this);
	}
	
	@Override
	protected void onCreate() {
			 aStorage=(Label)getFellow("aStorage");
			 aUsed=(Label)getFellow("aUsed");
			 aPayed=(Label)getFellow("aPayed");
			 aAvailable=(Label)getFellow("aAvailable");
			 aLivingTime=(Label)getFellow("aLivingTime");
			 aLeavingTime=(Label)getFellow("aLeavingTime");
			
			roomName=(Textbox)getFellow("roomName");
			storage =(Label)getFellow("storage");
			used =(Label)getFellow("used");
			payed =(Label)getFellow("payed");
			livingTime=(Textbox)getFellow("livingTime");
			leaveTime=(Textbox)getFellow("leaveTime");
			available=(Label)getFellow("available");

			adjustPrice=(Textbox)getFellow("adjustPrice");
			
			roomType=(Label)getFellow("roomType");
			openType=(Label)getFellow("openType");
	
			paymentMethod=(Listbox)getFellow("paymentMethod");
			invoiceListbox=(Listbox)getFellow("invoiceListbox");
			selectLivingList=(Listbox)getFellow("selectLivingList");
			mCardNumber=(Textbox)getFellow("mCardNumber");
			
			opTip=(Label)getFellow("opTip");
			okBtn=(Button)getFellow("okBtn");
			memberGrid=(Grid)getFellow("memberGrid");

			aAccount=(Textbox)getFellow("aAccount");
			account=(Label)getFellow("account");
			
			memberGrid.setVisible(false);
			selectLivingList.addEventListener(Events.ON_SELECT, new EventListener(){
				   public void onEvent(Event event) {

					   initMoney();
				   }
			   });

			paymentMethod.addEventListener(Events.ON_SELECT, new EventListener(){
				   public void onEvent(Event event) {
					   //PayMethod payMethod=UIUtils.getSelectedValue(paymentMethod);
					   String sPayMetod =UIUtils.getSelectedValue(paymentMethod);
					   if(sPayMetod.equals(PayMethod.MEMBER_CARD.name()))
					   {
						   memberGrid.setVisible(true);
						  // targetGrid.setVisible(false);
					   }else{
						   memberGrid.setVisible(false);
						  // targetGrid.setVisible(true);
						   okBtn.setDisabled(false);
					   }
					   
				   }
			   });
			init();
	}
	public void init(){
		initAllLiviingListbox();
		initMoney();
		
	}
	public void initAllLiviingListbox()
	{
		if(selectLive==null)
		{
			UIUtils.alert("无效的入住!请选择入住记录");
			this.detach();
		}
		selectLive =incomeService.queryBySelf(selectLive);
		this.setTitle("共选择了"+selectLive.size()+"记录");
		selectLivingList.getItems().clear();
		int i=0;
		for(Living l:selectLive)
		{
			Listitem listitem = new Listitem();
			listitem.setParent(selectLivingList);
			listitem.setValue(l);
			if(i==0){
		         setEarlyTime(l.getLiveTime());//必须先
		         setCheckTime(new Date());
			     aLivingTime.setValue(DateUtil.toString(l.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT));
			     aLeavingTime.setValue(DateUtil.toString(checkTime, DateUtil.HOTEL_DEFAULT_FORMAT));
				 listitem.setSelected(true);
			}
			if(l.getMember()!=null){
				UIUtils.setSelectedItem(paymentMethod, PayMethod.MEMBER_CARD.name());
				memberGrid.setVisible(true);
			}
			listitem.setLabel("["+l.getRoom().getName()+"]"+l.getCustormName());
			i++;
		}
	}
	//3初始化各金额
	public void initMoney()
	{
		selectLive =incomeService.queryBySelf(selectLive);

		InComeUtil.countInComeHours(selectLive, aStorage, aUsed, aPayed, aAvailable,1,aAccount);
		//个人账户
		Living sL =(Living)UIUtils.getSelectedValue(selectLivingList);
		//不变的
		roomName.setValue(sL.getRoom().getName());
		roomType.setValue(sL.getRoom().getType().getName());
		openType.setValue(OpenRoomType.valueOf(sL.getOpenRoomType()).getDisplayName());
		adjustPrice.setValue(Utils.toPrice(sL.getAdjustPrice()));
		livingTime.setValue(DateUtil.toString(sL.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT));
		leaveTime.setValue(DateUtil.toString(checkTime,DateUtil.HOTEL_DEFAULT_FORMAT));

		//accoutTime.setValue(String.valueOf(Logic.countUnionHours(this.checkTime, sL.getLiveTime())));
		//变动的
		InComeUtil.countInComeHours(sL, storage, used, payed, available,1,account);
	}
	public void checkOut()
	{
		if(!Hotel.onDuty()){
			UIUtils.alert("对不起你还未值班,不能操作此业务 !");
			return ;
		}
		if(!incomeService.isCheckedOut(selectLive)){
			return ;
		}
		if(!InComeUtil.isNoBorrow(selectLive)){
			return ;
		}
		StringBuffer msg = new StringBuffer();
		StringBuffer rMsg = new StringBuffer();
		StringBuffer finalAlert =new StringBuffer();
//		   finalAlert.append("谢谢你的光临");
		rMsg.append(InComeUtil.getAutoLivingReMark(selectLive,true));
		Living cur =(Living)UIUtils.getSelectedValue(selectLivingList);
		Living nCur=incomeService.get(Living.class, cur.getId());//防止长期没结而被其它人结了
		   msg.append("您好:").append(nCur.getCustormName()).append("\n");
		  // msg.append("开房方式:").append(OpenRoomType.valueOf(cur.getOpenRoomType()).getDisplayName()).append("\n");
		String rUnion="";
		String orderNo=DateUtil.toString(this.earlyTime, "yyyyMMddHHmmss");
		String serialNumber="";
		String checkOutNo="";
		int iUnion=selectLive.size();
		if(iUnion>1){
			rUnion="联房";
			serialNumber=Global.getLivineSerialNumber();
			msg.append("结账种类：联房").append("\n");
			msg.append("房间:\n").append(rMsg.toString()).append("\n");
		}else{
			rUnion="单人";
			msg.append("结账种类:单人").append("\n");
			msg.append("房间：").append(rMsg.toString()).append("\n");
		}
		
		String sPayMethod=UIUtils.getSelectedValue(paymentMethod);
		   msg.append("结账方式：").append(PayMethod.valueOf(sPayMethod).getDisplayName()).append("\n");
		//int storage=incomeService.querySavings(selectLive);//总存款
		int storage =incomeService.countAllStorages(selectLive, false);
		int comsum=incomeService.countCurCumsums(selectLive);//除加收外的消费
		
		int iAccount=Utils.toPrice(aAccount.getValue());
		TbAccounts ac =null;

		if(iAccount!=0){

			  msg.append("金额:").append(Utils.toPrice(iAccount)).append("\n");
			  ac = new TbAccounts();
			  ac.setLiving(nCur);
			  ac.setType(InComeItemType.FF.name());
			  ac.setCounts(1);
			  ac.setPrice(-iAccount);
			  ac.setPrice2(iAccount/iUnion);
			  ac.setName(InComeItemType.FF.getName());
			  ac.setEndstate("是");
			  ac.setEndtime(checkTime);
			  ac.setOpdate(DateUtil.toString(checkTime, "yyyy-MM-dd"));
			  ac.setOptime(new Date());
			  ac.setOpUser(Hotel.getCurrentUser().getName());
			  ac.setRemark(rUnion+"钟点"+InComeUtil.getAutoLivingReMark(selectLive,false)+"共计:"+Utils.toPrice(iAccount));
		}
		
		int allComsum =comsum+iAccount;//总消费
		int pay=storage-allComsum;//应付
		int finalPay =0;//最终应付
		msg.append("总存额：").append(Utils.toPrice(storage)).append("\n");
		msg.append("已消费：").append(Utils.toPrice(allComsum)).append("\n");
		if(pay==0){
			msg.append("本次应付:").append("0").append("\n");
			finalPay=0;
		}else if(pay<0){
			msg.append("本次应付:").append(Utils.toPrice(-pay)).append("\n");
			finalPay=-pay;
		}else{
			msg.append("本次退返:").append(Utils.toPrice(pay)).append("\n");
			finalPay=pay;
		}
		//会员增加记录
		User user =Hotel.getCurrentUser();
		Memberchargehistrory mch =null;
		if(sPayMethod.equals(PayMethod.MEMBER_CARD.name()))
		{
			//TODO
			if(storage!=0){
				UIUtils.alert("你在入住时已有预存:"+Utils.toPrice(storage)+"不能用会员卡结账!");
				return ;
			}
			String sCardNumber=mCardNumber.getValue();
			Member m =memberService.queryMemberOnCard(sCardNumber);
			if(m==null){
				UIUtils.alert("请用有效的会员卡结账!");
				return ;
			}

			  int mExceed =memberService.exceedBalance(m,finalPay,false);
			  int mBalance =memberService.queryMemberBalance(m);
			  if(mExceed<0){
				  UIUtils.alert("当前会员余额不够！当前余额为："+Utils.toPrice(mBalance));
				  return ;
			  }
		
				mch =new Memberchargehistrory();
				mch.setMember(m);
				mch.setChargeTime(checkTime);
				mch.setPrice(-allComsum);
				mch.setChargetype("非自助");
				mch.setOptype(ChargeOpType.OnConsume.name());
				mch.setRemark(DateUtil.toString(checkTime, DateUtil.HOTEL_DEFAULT_FORMAT)+":"+rUnion+rMsg.toString());
				checkOutNo=m.getCardNo();
			
		}else if(sPayMethod.equals(PayMethod.CASH.name())){
			checkOutNo=orderNo;
			
		}else
		{
			finalAlert.append("当前是用").append(PayMethod.valueOf(sPayMethod).getDisplayName()).append("结账").append("\n");
			finalAlert.append("请结算人员"+user.getName()+"在结账完成后完成输入银行凭证单据号");
		}
		int invoice=Integer.valueOf((String)UIUtils.getSelectedValue(invoiceListbox));
		if(invoice==0){
			msg.append("未开票").append("\n");
		}else{
			msg.append("已开票").append("\n");
			finalAlert.append("请给客人开票").append("\n");
			}
		TbCloseAccounts ca = new TbCloseAccounts();
		ca.setLiving(nCur);
		ca.setInvoice(invoice);
		ca.setTime(new Date());
		ca.setAccounts(allComsum);
		ca.setNeedAccounts(InComeUtil.sysInComeHalfDay(selectLive));
		ca.setType(PayMethod.valueOf(sPayMethod).name());
		
		ca.setUsername(user.getName());
		ca.setUserId(user.getId());
		ca.setOrderNo(orderNo);
		ca.setSerialNumber(serialNumber);
		ca.setCheckOutNo(checkOutNo);
        if(UIUtils.confirm(msg.toString())){
        	incomeService.saveAllCheck(selectLive,ca,ac, mch,pay);
        	opTip.setValue("结账成功!");
        	if(finalAlert.length() >0)
        	   UIUtils.alert(finalAlert.toString());
        	okBtn.setDisabled(true);
        	success=true;
		}
	}
	public Date getEarlyTime() {
		return earlyTime;
	}
	public void setEarlyTime(Date earlyTime) {
		this.earlyTime = earlyTime;
	}
	public Date getCheckTime() {
		return checkTime;
	}
	public void setCheckTime(Date checkTime) {
		this.checkTime = checkTime;
	}
}
